<?php
class ControlHelp
{
    public static $pageIndes ;
    public static $tatolnums ;
    public static $pageSize ;
    public static $tatolpages;
    protected $db = NULL;
    public function __construct()
    {              
        $this->db = Master::connectToDB();
        self::$pageSize = Config::$Page ;
    }

    public function setDomainClass($className)
    {
      $this->setClassName($className);
    }

    public function  __call($functionName,  $functionArguments)
    {         
      return Master::initFunction($this->db, $functionName, $functionArguments);
    }     
    public function SaveModel($Table)
    {     
        $fields =  $this->listFields($Table); 
        $str = array();
        $uparr = array();
        $isUpdate = false;
        $priKey = "";
        $entyf = array('password');//;explode(',',$md5Fileds);
        foreach($fields as $key)
        {
            if ($key['Key']=='PRI') $priKey = $key['Field'];
            foreach($_REQUEST as $k=>$v)
            {
                if (in_array($k,$entyf))
                {
                    if (strlen($v)<16)
                        $v = md5($v); 
                }
                if ($key['Field']=='createtime'){
                   $str['createtime'] =date("Y-m-d h:m:s",time());
                }                   
                if ($key['Field']==$k)
                    $str[$key['Field']] =$v;
                if ($priKey!='' && $k==$priKey ){
                    $isUpdate = true;
                    $uparr[$priKey] = $v;
                }             
            }
        } 
        if ($isUpdate)
            return  $this->update($Table,$str, $uparr);
        else
            return  $this->insert($Table,$str);
    }
    
    public function SaveModel_1($groupid)
    {    
        foreach($_REQUEST as $k=>$v)
        { 
            $node = $this->select('admins_node',array('names'=>$k));
            if (count($node)>0)
                $this->insert('admins_access',array('groupid'=>$groupid,'nodeid'=>$v,'level'=>0,'parentNodeId'=>0,'status'=>0));
        } 
    }    
    
	//'********************************
	//'非上传图片表单拷贝处理
	//'********************************    
    public static function NoUpload($Req,$Table)
    {
        return "";
    }
    
	//'********************************
	//'上传图片表单拷贝处理
	//'********************************    
    public static function Upload($Req,$Table)
    {
        return "";
    }    
    
    public function listModel($tableName,$whstr=array(),$showPage=true){
        self::$tatolnums = $this->selectTatos($tableName,$whstr);  
        $indexpage = Get::getByKey('page');           
        $order = '';     
        if ($indexpage==null || $indexpage=="")
            $indexpage= 1;
        if ($indexpage > 0)
        {
            $offset = ($indexpage-1) * self::$pageSize;
            $order = 'order by id desc limit  '.$offset .','.self::$pageSize;
        }
        if (!$showPage)
             $order ="";
        return $this->select($tableName,$whstr,array('*'),$order);        
    }
    
    public function PageUrl()
    {
        $pageStr = '<div id="pagination" class="pagination">'."\r\n";
        self::$tatolpages = ceil(self::$tatolnums / self::$pageSize);   
        if (self::$tatolpages>1)
        {    
            $pageStr=$pageStr.'<a href="'.self::setUrl(1).'" title="首页">&laquo; 首页</a>'."\r\n";
            $pageStr=$pageStr.'<a href="'.self::setUrl("pre").'" title="上一页">&laquo; 上一页</a>'."\r\n";
                for($i=1;$i<=self::$tatolpages;$i++)
                    $pageStr=$pageStr.'<a href="'.self::setUrl($i).'" class="number" title="'.$i.'">'.$i.'</a>'."\r\n";
            $pageStr=$pageStr.'<a href="'.self::setUrl('next').'" title="下一页">下一页 &raquo;</a>'."\r\n";
            $pageStr=$pageStr.'<a href="'.self::setUrl(self::$tatolpages).'" title="尾页">尾页 &raquo;</a></div>'."\r\n";
            $pageStr=$pageStr.'<div class="clear"></div>'."\r\n";
            $page = Get::getByKey('page');
            $pageStr=$pageStr."<script type=\"text/javascript\">$(function() {\$(\"div#pagination>a[title='".$page."']\").addClass('current') });</script>";
        }else
            $pageStr = "";
        return $pageStr;
    }
    
    public function setUrl($page)
    {              
        if ($page=='pre')            
            $page = Get::getByKey('page') -1;
        else if ($page=='next')
            $page = Get::getByKey('page')?Get::getByKey('page')+1:2;
        if ($page<=1)
            $page = 1; 
        else if ($page>=self::$tatolpages)
            $page = self::$tatolpages; 
        $geturl = Get::getAll();
        $geturl['page'] = $page;
        $pageurl= "?";
        foreach($geturl as $key=>$v)
        {
            $pageurl = $pageurl.'&'.$key.'='.$v;
        }
        return $pageurl;
    }
    
    function checkRbac($nodename)
    {
        global $UserRbac ,$ComRbac,$AdminRbac;
        $CheckBool = false;
        if($AdminRbac)
        {
            if(Session::getByKey('adminname')!="")
            {
                $CheckBool = self::CompareNode($nodename,self::ReadRbacCache ("admins","admingroup"));
            }
            if (!$CheckBool)
            {
                echo "<script>alert(\"提示:您没有此操作的执行权限\");history.go(-1)</script>";
                exit();
            }
        }
    }
    public function CompareNode($Nodename,$RbacList)
    {
        $CompareNode = false;
        foreach($RbacList as $k)
        {
            if ($Nodename ==$k['names'] )
                $CompareNode = true;
            
        }
        return $CompareNode;
    }
    
    public function ReadRbacCache($roleTable,$groupid)
    {      
        $groupid = Session::getByKey($groupid);
        $res =  $this->execute("select b.* from ".$roleTable."_access a left join ".$roleTable."_node b on a.nodeid = b.id where a.groupid = ".$groupid);
        return $res;
    }    
}
?>
